package com.erhirent.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

import com.erhairent.tools.CarUpdate;
import com.erhairent.tools.DBHelp;
import com.erhairent.tools.ResultSetHandler;
import com.erhairent.tools.Template;
import com.erhirent.bean.Brand;
import com.erhirent.bean.Car;
import com.erhirent.bean.Record;
import com.erhirent.bean.Type;

import com.erhirent.dao.IcarDao;

public class CarDaoImpl implements IcarDao {

	CarUpdate cp = new CarUpdate();

	Template<List<Car>> tCar = new Template<>();

	//private Savepoint point;

	// 预约
	@Override
	public boolean carBook(int x) {
		return cp.updateSQL("update erhai_information set car_isorder = '不可预约' where car_id = ?", x);

	}

	// 解除预约
	@Override
	public boolean clearBook(int y) {
		return cp.updateSQL("update erhai_information set car_isorder = '可预约'  where car_id = ?", y);

	}

	// 修改上下架
	@Override
	public boolean changeCarputaway(String putaway, int num) {

		return cp.updateSQL("update erhai_information set car_isputaway = ? where car_id = ?", putaway, num);

	}

	// 修改价格
	@Override
	public boolean changeCarPrice(double pri, int num) {
		return cp.updateSQL("update erhai_information set car_price = ?  where car_id = ?", pri, num);

	}

	// 添加汽车
	@Override
	public boolean addCar(String name, String remark, int brand, int type, double payday, String car_number,
			String color, double value) {

		return cp.updateSQL(
				"insert into erhai_information(car_id,car_name,car_remark,car_brand_id,car_type_id,car_price,car_number,car_color,car_value) values (car_seq.nextval,?,?,?,?,?,?,?,?)",
				name, remark, brand, type, payday, car_number, color, value);

	}

	// 删除记录 修改状态
	@Override
	public boolean deleterecord(int carID) {
		Connection conn = null;
		try {
			conn = DBHelp.getInstance().getConnection();
			conn.setAutoCommit(false);
			String sql1 = "delete from return_record where return_car_id = ?";
			String sql2 = "update erhai_information set car_isrent ='可借' where car_id = ?";
			PreparedStatement ps1 = conn.prepareStatement(sql1);
			PreparedStatement ps2 = conn.prepareStatement(sql2);

			ps1.setInt(1, carID);
			ps2.setInt(1, carID);

			int a = ps1.executeUpdate();
			int b = ps2.executeUpdate();

			conn.commit();

			if (a > 0 && b > 0) {

				return true;
			}
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {

				return false;
			}

		}

		return false;
	}

	// 用户添加租车记录
	@Override
	public boolean userRecord(int carID, int userID, double pri, int day) {

		Connection conn = null;
		try {
			conn = DBHelp.getInstance().getConnection();
			conn.setAutoCommit(false);
			String sql1 = "insert into  erhai_rent_record(rent_id, rent_car_id, rent_user_id, rent_total_price, return_date) values (rent_seq.nextval,?,?,?,sysdate+?)";
			String sql2 = "update erhai_information set car_isrent ='不可借' where car_id = ?";
			String sql3 = "insert into return_record(return_user_id,return_car_id)values (?,?)";
			PreparedStatement ps1 = conn.prepareStatement(sql1);
			PreparedStatement ps2 = conn.prepareStatement(sql2);
			PreparedStatement ps3 = conn.prepareStatement(sql3);
			ps1.setInt(1, carID);
			ps1.setInt(2, userID);
			ps1.setDouble(3, pri);
			ps1.setInt(4, day);
			ps2.setInt(1, carID);
			ps3.setInt(1, userID);
			ps3.setInt(2, carID);

			//point= conn.setSavepoint();
			int a = ps1.executeUpdate();
			int b = ps2.executeUpdate();
			//System.out.println(1/0);
			int c = ps3.executeUpdate();
			conn.commit();

			if (a > 0 && b > 0 && c > 0) {

				return true;
			}
		} catch (Exception e) {
			try {
				conn.rollback();
				//conn.rollback(point);
				//conn.commit();
			} catch (SQLException e1) {

				return false;
			}

		}

		return false;

	}

	// ------------------------------------------------------------------

	// 价格降序
	@Override
	public void queryDesc() {

		List<Car> list = tCar.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent  from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id and car_isputaway = '上架' order by e.car_price desc",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));

								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				});

		System.out.println("---------------------------------------------");
		System.out.println("汽车ID\t" + "汽车名称\t" + "备注\t" + "品牌\t" + "类型\t" + "单价\t" + "是否可租");
		for (Car car : list) {
			System.out.print(car.getcarId() + "\t");
			System.out.print(car.getcarName() + "\t");
			System.out.print(car.getcarRemark() + "\t");
			System.out.print(car.getbrandName() + "\t");
			System.out.print(car.gettypeName() + "\t");
			System.out.print(car.getcarPrice() + "/天\t");
			System.out.println(car.getcarIsrent() + "\t");
		}
		System.out.println("---------------------------------------------");

	}

	// 价格升序

	@Override
	public void queryAsc() {

		List<Car> list = tCar.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent  from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id and car_isputaway = '上架' order by e.car_price asc ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));

								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				});

		System.out.println("---------------------------------------------");
		System.out.println("汽车ID\t" + "汽车名称\t" + "备注\t" + "品牌\t" + "类型\t" + "单价\t" + "是否可租");
		for (Car car : list) {
			System.out.print(car.getcarId() + "\t");
			System.out.print(car.getcarName() + "\t");
			System.out.print(car.getcarRemark() + "\t");
			System.out.print(car.getbrandName() + "\t");
			System.out.print(car.gettypeName() + "\t");
			System.out.print(car.getcarPrice() + "/天\t");
			System.out.println(car.getcarIsrent() + "\t");
		}
		System.out.println("---------------------------------------------");

	}

	// 所有类型
	@Override
	public List<Type> querytype() {
		Template<List<Type>> t = new Template<>();
		List<Type> list = t.testQuery("select type_id,type_name from t_type ", new ResultSetHandler<List<Type>>() {

			@Override
			public List<Type> handler(ResultSet rSet) {
				List<Type> list = new ArrayList<>();
				try {

					while (rSet.next()) {
						Type a = new Type();

						a.settypeId(rSet.getInt("type_id"));
						a.settypeName(rSet.getString("type_name"));

						list.add(a);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return list;
			}
		});

		return list;

	}

	// 根据类型查寻
	@Override
	public List<Car> typequery(int n) {

		List<Car> list = tCar.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent ,car_type_id from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id and car_isputaway = '上架' and t.type_id=? order by e.car_id ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));

								a.setcarTypeId(rSet.getInt("car_type_id"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, n);

		return list;

	}

	// 所有品牌
	@Override
	public List<Brand> querybrand() {
		Template<List<Brand>> t = new Template<>();
		List<Brand> list = t.testQuery("select brand_id,brand_name from brand ", new ResultSetHandler<List<Brand>>() {

			@Override
			public List<Brand> handler(ResultSet rSet) {
				List<Brand> list = new ArrayList<>();
				try {

					while (rSet.next()) {
						Brand a = new Brand();

						a.setBrandId(rSet.getInt("brand_id"));
						a.setBrandName(rSet.getString("brand_name"));

						list.add(a);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return list;
			}
		});

		return list;

	}

	// 根据品牌查询
	@Override
	public List<Car> brandquery(int m) {
		Template<List<Car>> t = new Template<>();

		List<Car> list = t.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent ,car_brand_id from brand b ,t_type t ,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id  and car_isputaway = '上架' and b.brand_id =? order by e.car_id",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));
								a.setcarBrandId(rSet.getInt("car_brand_id"));

								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, m);

		return list;

	}

	// 用户查询所有可借车辆
	@Override
	public List<Car> userquery() {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent  from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id  and car_isrent = '可借'and car_isorder = '可预约' and car_isputaway = '上架' order by e.car_id ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));

								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				});

		return list;

	}

	// 判断是否可租
	@Override
	public boolean userqueryRent(int a) {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select car_id,car_isrent from erhai_information where car_isputaway = '上架' and car_id = ?",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarIsrent(rSet.getString("car_isrent"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, a);

		for (Car car : list) {
			if (car.getcarIsrent().equals("可借")) {
				return true;
			}
		}

		return false;
	}

	// 用户查询一条租车记录
	@Override
	public List<Car> userqueryoneRecord(int userID) {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select  user_id,car_id,car_name,rent_total_price,car_remark,brand_name,type_name,rent_date,return_date from erhai_rent_record r,erhai_information i,brand b,t_type t,erhai_users u where  r.rent_car_id = i.car_id and r.rent_user_id = u.user_id and i.car_brand_id = b.brand_id and i.car_type_id = t.type_id and user_id = ? and rent_id =(select max(rent_id) from erhai_rent_record) ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();

								a.setuserId(rSet.getInt("user_id"));
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setrentTotalPrice(rSet.getDouble("rent_total_price"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setrentTime(rSet.getString("rent_date"));
								a.setreturnTime(rSet.getString("return_date"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, userID);

		return list;

	}

	// 用户查询自己的租车记录
	@Override
	public List<Car> userqueryRecord(int userID) {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select  user_id,car_id,car_name,rent_total_price,car_remark,brand_name,type_name,rent_date,return_date from erhai_rent_record r,erhai_information i,brand b,t_type t,erhai_users u where  r.rent_car_id = i.car_id and r.rent_user_id = u.user_id and i.car_brand_id = b.brand_id and i.car_type_id = t.type_id and user_id = ?",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();

								a.setuserId(rSet.getInt("user_id"));
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setrentTotalPrice(rSet.getDouble("rent_total_price"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setrentTime(rSet.getString("rent_date"));
								a.setreturnTime(rSet.getString("return_date"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, userID);

		return list;
	}

	// 获得汽车单价
	@Override
	public double getCarPrice(int a) {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery("select car_id ,car_price from erhai_information where car_id = ? ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarPrice(rSet.getDouble("car_price"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;

					}

				}, a);
		double carPrice = 0;
		if (list != null) {
			for (Car car : list) {
				carPrice = car.getcarPrice();
			}

		}

		return carPrice;
	}

	// 管理员查询指定
	@Override
	public void manergerquery(int j) {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent ,car_isputaway from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id  and e.car_id = ? order by e.car_id ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));
								a.setcarIsputaway(rSet.getString("car_isputaway"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, j);
		System.out.println("---------------------------------------------");
		System.out.println("汽车编号\t" + "汽车名称\t" + "备注\t" + "品牌\t" + "类型\t" + "单价\t" + "是否可租\t" + "是否上架");

		if (list != null) {
			for (Car car : list) {
				System.out.print(car.getcarId() + "\t");
				System.out.print(car.getcarName() + "\t");
				System.out.print(car.getcarRemark() + "\t");
				System.out.print(car.getbrandName() + "\t");
				System.out.print(car.gettypeName() + "\t");
				System.out.print(car.getcarPrice() + "/天\t");
				System.out.print(car.getcarIsrent() + "\t");
				System.out.println(car.getcarIsputaway() + "\t");
			}

		}
		System.out.println("---------------------------------------------");

	}

	// 管理员查询所有
	@Override
	public List<Car> queryAll() {

		List<Car> list = tCar.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent ,car_isputaway from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id order by e.car_id ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));
								a.setcarIsputaway(rSet.getString("Car_isputaway"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				});

		return list;

	}

	// 查询可修改车
	@Override
	public List<Car> queryChange() {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent ,car_isputaway from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id and car_isrent = '可借' order by e.car_id ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));
								a.setcarIsputaway(rSet.getString("Car_isputaway"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				});

		return list;

	}

	// 需要修改的车
	@Override
	public List<Car> queryChange(int num) {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent ,car_isputaway from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id and car_isrent = '可借' and e.car_id = ? order by e.car_id ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));
								a.setcarIsputaway(rSet.getString("Car_isputaway"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, num);

		return list;

	}

	// 管理员查询记录
	@Override
	public List<Record> manegerqueryRecord() {
		Template<List<Record>> t = new Template<>();
		List<Record> list = t.testQuery(
				"select rent_id,car_id,car_name,user_id,user_name,car_price,rent_total_price,car_remark,brand_name,type_name,rent_date,return_date from erhai_rent_record r,erhai_information i,brand b,t_type t,erhai_users u where  r.rent_car_id = i.car_id and r.rent_user_id = u.user_id and i.car_brand_id = b.brand_id and i.car_type_id = t.type_id",
				new ResultSetHandler<List<Record>>() {

					@Override
					public List<Record> handler(ResultSet rSet) {
						List<Record> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Record a = new Record();

								a.setrentId(rSet.getInt("rent_id"));
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setuserId(rSet.getInt("user_id"));
								a.setuserName(rSet.getString("user_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setrentTotalPrice(rSet.getDouble("rent_total_price"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setrentTime(rSet.getString("rent_date"));
								a.setreturnTime(rSet.getString("return_date"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				});

		return list;

	}

	// 预约车是否上架且不可租
	@Override
	public List<Car> userqueryPutaway() {
		Template<List<Car>> t = new Template<>();
		List<Car> list = t.testQuery(
				"select e.car_id ,e.car_name,e.car_remark,b.brand_name,t.type_name,e.car_price,e.car_isrent ,car_isorder  from brand b ,t_type t,erhai_information e where e.car_brand_id = b.brand_id and e.car_type_id = t.type_id  and car_isputaway = '上架' and car_isrent = '不可借' order by e.car_id ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarName(rSet.getString("car_name"));
								a.setcarRemark(rSet.getString("Car_remark"));
								a.setbrandName(rSet.getString("Brand_name"));
								a.settypeName(rSet.getString("type_name"));
								a.setcarPrice(rSet.getDouble("car_price"));
								a.setcarIsrent(rSet.getString("car_isrent"));
								a.setcarIsorder(rSet.getString("car_isorder"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				});

		return list;

	}

	// 预约车是否已被预约
	@Override
	public boolean userqueryOrder(int x) {

		List<Car> list = tCar.testQuery(
				"select car_id,car_isorder from erhai_information where car_isputaway = '上架' and car_isrent = '不可借' and car_id = ? ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("car_id"));
								a.setcarIsorder(rSet.getString("car_isorder"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, x);

		for (Car car : list) {
			if (car.getcarIsorder().equals("可预约")) {
				return true;
			}
		}

		return false;
	}

	// 车是否可还
	@Override
	public boolean isreturn(int carID, int userID) {
		List<Car> list = tCar.testQuery(
				"select return_user_id,return_car_id from return_record where return_user_id = ? and return_car_id = ? ",
				new ResultSetHandler<List<Car>>() {

					@Override
					public List<Car> handler(ResultSet rSet) {
						List<Car> list = new ArrayList<>();
						try {

							while (rSet.next()) {
								Car a = new Car();
								a.setcarId(rSet.getInt("return_car_id"));
								a.setuserId(rSet.getInt("return_user_id"));
								list.add(a);
							}
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return list;
					}
				}, userID, carID);
		if (list.size() != 0) {
			return true;
		}

		return false;
	}

}
